package com.weiter.web.controller.monitor;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiter.common.core.core.controller.BaseController;
import com.weiter.common.core.core.domain.AjaxResult;
import com.weiter.common.core.core.domain.entity.SysOperLog;
import com.weiter.common.core.core.page.PageQuery;
import com.weiter.common.core.core.page.TableDataInfo;
import com.weiter.common.core.utils.poi.ExcelUtil;
import com.weiter.common.log.annotation.Log;
import com.weiter.common.log.enums.BusinessType;
import com.weiter.system.service.ISysOperLogService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 操作日志
 * @author weiter
 */
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperlogController extends BaseController {

    @Resource
    private ISysOperLogService operLogService;

    /**
     * 查询操作日志分页列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/list")
    public TableDataInfo<SysOperLog> list(SysOperLog query, PageQuery pageQuery) {
        Page<SysOperLog> page = operLogService.selectOperLogPage(query, pageQuery);
        return TableDataInfo.build(page);
    }

    /**
     * 导出操作日志
     */
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog) {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<>(SysOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    /**
     * 删除操作日志
     */
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds) {
        return toAjax(operLogService.removeBatchByIds(Arrays.asList(operIds)));
    }

    /**
     * 清空操作日志
     */
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        operLogService.cleanOperLog();
        return success();
    }
}
